<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta content="width=device-width, initial-scale=1, maximum-scale=1" name="viewport">
    <title>开始使用 layui</title>
    <meta content="*" http-equiv="Access-Control-Allow-Origin">
    <link href="/layui/css/layui.css" rel="stylesheet">
</head>
<body>
<!-- 搜索栏 -->
<div class="searchAnnouncement">
    名称：
    <div class="layui-inline">
        <input type="text" class="layui-input" name="name" id="name" autocomplete="off" />
    </div>
    <button class="layui-btn" id="searchBtn" data-type="reload">搜索</button>
</div>
<!-- 表格 -->
<table id="announcement" lay-filter="announcement"></table>
<!-- 表头信息 -->
<div id="toolbar" style="display: none;">
    <div class="layui-btn-group">
        <button class="layui-btn layui-btn-sm" lay-event="add">新增</button>
    </div>
</div>
<!--操作栏信息-->
<script id="operatebar" type="text/html">
    <a class="layui-btn layui-btn-sm" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-sm layui-btn-danger" lay-event="delete">删除</a>
</script>
<!--新增、修改页面（模态框）
新增和修改最大的差异是：新增表单项中是没有数据的，修改的表单项中是存在数据
-->
<div id="modal" style="display: none;">
    <form class="layui-form" lay-filter="entity">
        <input name="id" style="display: none;" type="text"/>
        <div class="layui-form-item">
            <label class="layui-form-label">标题</label>
            <div class="layui-input-block">
                <input autocomplete="off" class="layui-input" lay-verify="required" name="title" placeholder="请输入标题" required="" type="text">
            </div>
        </div>
        <div class="layui-form-item layui-form-text">
            <label class="layui-form-label">内容</label>
            <div class="layui-input-block">
                <textarea class="layui-textarea" name="contents" placeholder="请输入内容"></textarea>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">状态</label>
            <div class="layui-input-block">
                <input checked="" name="annStatus" title="正常" type="radio"
                       value="0">
                <div class="layui-unselect layui-form-radio"><i class="layui-anim layui-icon"> </i>
                    <div>正常</div>
                </div>
                <input name="annStatus" title="废弃" type="radio" value="1">
                <div
                        class="layui-unselect layui-form-radio layui-form-radioed"><i class="layui-anim layui-icon"> </i>
                    <div>废弃</div>
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-filter="addandupdate" lay-submit="">立即提交</button>
                <button class="layui-btn layui-btn-primary" type="reset">重置
                </button>
            </div>
        </div>
    </form>
</div>
<script src="/layui/layui.js"></script>
<script>
    layui.use(['table','layer','jquery','form'], function(){
        var table = layui.table;
        var layer = layui.layer;
        var $ = layui.jquery;
        var form = layui.form;
        table.render({
            elem: '#announcement'
            ,id : 'testReload'
            ,height: 472
            ,url: '/api/announcement/page' //数据接口
            ,page: true //开启分页
            ,cols: [[ //表头
                {field: 'id', title: 'ID', sort: true, fixed: 'left'}
                ,{field: 'title', title: '标题'}
                , {field: 'contents', title: '内容'}
                ,{field: 'contents', title: '内容'}
                ,{field: 'createDatetime', title: '创建时间', sort: true}
                ,{field: 'annStatus', title: '状态', templet: function (d) {
                        return d.annStatus === 0 ? "正常":"废弃";
                    }}
                ,{field: 'annDate', title: '修改日期', sort: true}
                ,{title: '操作', toolbar : '#operatebar', align:'center'}
            ]]
            , toolbar : '#toolbar'
            , response : {
                statusCode : 1
            }
            , parseData : function (res) {
                if (res.status == 1) {
                    return {
                        "code":res.status,
                        "msg": res.message,
                        "count": res.total,
                        "data": res.data
                    }
                }
            }
        });
        // 监控表格的工作栏
        table.on("toolbar(announcement)", function (obj) {
            switch (obj.event) {
                case 'add':
                    open2("新增公告",null);
                    break;
            }
        });
        // 监控表格操作栏
        table.on("tool(announcement)", function (obj) {
            var data = obj.data;
            switch (obj.event) {
                case 'delete':
                    layer.confirm("确定要删除该数据吗？", function (index) {
                        // 执行删除操作
                        $.ajax({
                            url:'/api/announcement/'+ data.id,
                            type:'delete',
                            dataType: 'json',
                            success: function (res) {
                                if(res.status == 1) {
                                    layer.msg(res.message);
                                //active.reload();
                                    obj.del();
                                } else {
                                    layer.msg(res.message);
                                }
                            }
                        });
                        // 对话框关闭掉
                        layer.close(index);
                    });
                    break;
                case 'edit':
                    open2("修改公告", data);
                    break;
            }
        });
        function open2(title, entity){
            // 向表单中赋值
            form.val('entity', entity);
            layer.open({
                type : 1
                ,title : title
                ,content : $('#modal')
                ,offset: '10px'
                ,area: ['600px', '80%']
                ,end : function () {
                //table.reload('testReload')
                    active.reload();
                }
            });
        }
        // 当用户输入完信息后，点击了提交按钮，执行以下操作
        form.on("submit(addandupdate)", function () {
            // 从表单中取值
            var data = form.val("entity");
            var type = data.id ? "put" : "post";
            console.log(data);
            $.ajax({
                url:'/api/announcement',
                type: type,
                dataType:'json',
                contentType:'application/json',
                data: JSON.stringify(data),
                success: function (res) {
                    if (res.status == 1) {
                        layer.msg(res.message,{time:1*1000}, function() {
                            layer.closeAll();
                        });
                    } else {
                        layer.msg(res.message);
                    }
                }
            });
            return false;
        });
            var active= {reload : function(){
                var name = $('#name');
                // 重新加载表
                    table.reload('testReload', {
                        page:{
                            curr : 1
                        }
                        ,where : {
                            "title" : name.val()
                        }
                    })
                }
            };
        $('.searchAnnouncement .layui-btn').on('click', function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });
    });
</script>
</body>
</html>